package com.example.memory.servicde.impl;

import com.example.memory.base.entity.SQLBuilder;
import com.example.memory.base.service.impl.BaseServiceImpl;
import com.example.memory.dao.ContentDao;
import com.example.memory.entity.Content;
import com.example.memory.entity.File;
import com.example.memory.servicde.ContentService;
import com.example.memory.servicde.FileService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class ContentServiceImpl extends BaseServiceImpl<Content> implements ContentService {
    @Resource
    ContentDao contentDao;

    @Override
    public List<String> getYear() {
        return contentDao.getYear();
    }

    @Override
    public List<String> getDay(String year) {
        return contentDao.getDay(year);
    }

    @Resource
    FileService fileService;

    @Override
    public Object getDayList(Content content) {
        List<Content> list = super.getList(content);
        Map<String, List<Content>> map = new LinkedHashMap<>();
        for (Content c : list) {
            String k = c.getTime().substring(5, 10);
            if (!map.containsKey(k)) {
                List<Content> temp = new ArrayList<>();
                map.put(k, temp);
            }
            map.get(k).add(c);
            List<File> files = fileService.selectList(SQLBuilder.getInstance().eq("cid", c.getId()));
            c.setFileList(files);
        }
        return map;
    }
}
